﻿using HZY.Framework.DependencyInjection;
using Long.BLL;
using Long.Helper;
using MiniExcelLibs;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Long.SpraySCADA
{
    public partial class PageReportManage : UIPage, ISingletonSelfDependency
    {
        private readonly DataManager _dataManager;
        private BaseResult<DataQuaeryResulyDto> res;
        public PageReportManage(DataManager dataManager)
        {
            InitializeComponent();
            _dataManager = dataManager;

            this.dtp_Start.Value = DateTime.Now.AddDays(-1);
            this.dtp_End.Value = DateTime.Now;
        }

        private async void btn_QueryData_Click(object sender, EventArgs e)
        {
            DateTime startTime = this.dtp_Start.Value;
            DateTime endTime = this.dtp_End.Value;
            if (startTime >= endTime)
            {
                MessageBox.Show("开始时间不能大于结束时间！");
                return;
            }
            res = await _dataManager.GetDataListByTimeAsyncManager(new DataQueryDto() { StartTime = startTime, EndTime = endTime });
            if (res.ResultStatus == SystemEnums.ResultStatus.Success)
            {
                //设置分页总数
                pgn_Data.TotalCount = res.Data.Count;
                //设置每页数量
                pgn_Data.PageSize = 15;
                //设置当前页
                pgn_Data.ActivePage = 1;
                //绑定数据

            }
        }

        private void pgn_Data_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            List<DataQuaeryResulyDto> data = res.Data.Skip(pageIndex * count).Take(count).ToList();
            //绑定数据
            this.dgv_Data.DataSource = null;
            this.dgv_Data.DataSource = data;
        }

        private void btn_ExportData_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel 文件(*.xlsx)|*.xlsx";
            sfd.FileName = "SprayReport_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string fileName = sfd.FileName;
                    List<DataQuaeryResulyDto>? dataExcel = this.dgv_Data.DataSource as List<DataQuaeryResulyDto>;

                    MiniExcel.SaveAs(fileName, dataExcel);
                    UIMessageBox.ShowSuccess("导出成功！");
                }
                catch (Exception ex)
                {
                    UIMessageBox.ShowSuccess("导出成功：" + ex.Message);
                    LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
                }
            }
        }
        #region 窗体切换卡顿
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams paras = base.CreateParams;
                paras.ExStyle |= 0x02000000;
                return paras;
            }
        }
        #endregion
    }
}
